package com.rimi.service.impl;

import com.rimi.commons.CommonResult;
import com.rimi.commons.ResultStatus;
import com.rimi.mapper.OrderMapper;
import com.rimi.model.dto.OrderGoodsDto;
import com.rimi.model.vo.PaymentVo;
import com.rimi.model.vo.RefundVo;
import com.rimi.service.OrderService;
import com.rimi.util.CurrentTimeUtil;
import com.rimi.util.OrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * @author luka
 * @version V1.0
 * @date 2021 2021/7/7 0007 11:51
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Override
    public CommonResult addOrder(Integer userId, Integer goodId, Integer goodQuantity) {

        // 生成订单编号
        String orderNo = OrderUtil.getOrderNo();

        // 商品总价
        double totalPrice = orderMapper.findPriceByGoodId(goodId) * goodQuantity;

        //当前时间
        String currentTime = CurrentTimeUtil.currentTime();

        int row = orderMapper.addOrder(userId,orderNo,goodId,goodQuantity,totalPrice,currentTime);
        
        return row != 0 ? CommonResult.onSuccess(null) : CommonResult.onFailed(ResultStatus.ADD_FAILED);
    }

    @Override
    public List<OrderGoodsDto> orderGoods(Integer userId,String function) {
        return orderMapper.findOrderGoodsByUserId(userId,function);
    }

    @Override
    public CommonResult paid(String orderNum, String tradeNo, Date paymentTime, Double paymentPrice,String defaultAddressInfo) {

        int row = orderMapper.paid(orderNum,tradeNo,paymentTime,paymentPrice,defaultAddressInfo);

        return row != 0 ? CommonResult.onSuccess(null) : CommonResult.onFailed(ResultStatus.ADD_FAILED);
    }

    /**
     * 根据订单id查询订单
     * @param orderId
     * @return
     */
    @Override
    public PaymentVo findOrderByOrderId(Integer orderId,Integer userId) {
        return orderMapper.findOrderByOrderId(orderId,userId);
    }
    /**
     * 根据订单id查询需要退款的订单
     * @param orderId
     * @return
     */
    @Override
    public RefundVo findOrderRefundByOrderId(Integer orderId,Integer userId) {
        return orderMapper.findOrderRefundByOrderId(orderId,userId);
    }
    /**
     * 修改订单退款状态为退款中，并修改退款类型，金额，原因，图片
     * @param refundVo
     * @return
     */
    @Override
    public int updateRefundOrderStatusForDuring(RefundVo refundVo) {
        return orderMapper.updateRefundOrderStatusForDuring(refundVo);
    }
    /**
     * 修改订单退款状态为退款成功
     * @param refundVo
     * @return
     */
    @Override
    public int updateRefundOrderStatusForSuccess(RefundVo refundVo) {
        return orderMapper.updateRefundOrderStatusForSuccess(refundVo);
    }

    /**
     * 确认收货，订单状态变为待评价
     * @param orderId
     * @return
     */
    @Override
    public CommonResult received(Integer orderId) {
        int row = orderMapper.received(orderId);

        return row != 0 ? CommonResult.onSuccess(null) : CommonResult.onFailed(ResultStatus.ADD_FAILED);
    }

    @Override
    public CommonResult cancelOrder(Integer orderId) {
        int row = orderMapper.cancelOrder(orderId);

        return row != 0 ? CommonResult.onSuccess(null) : CommonResult.onFailed(ResultStatus.ADD_FAILED);
    }

    @Override
    public CommonResult evaluated(Integer orderId) {

        int rowEvaluate = orderMapper.evaluated(orderId);

        return rowEvaluate != 0 ? CommonResult.onSuccess(null) : CommonResult.onFailed(ResultStatus.ADD_FAILED);
    }

    /**
     * 修改订单退款状态为退款失败
     * @param refundVo
     * @return
     */
    @Override
    public int updateRefundOrderStatusForFail(RefundVo refundVo) {
        return orderMapper.updateRefundOrderStatusForFail(refundVo);
    }
}
